package newx.com.control.button
{
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.HorizontalLayout;
	import newx.com.control.IButton;
	import newx.com.control.IClip;
	import newx.com.control.NxClip;
	import newx.com.control.VerticalLayout;
	
	/**
	 * 普通按钮
	 * @author lw
	 * 
	 */	
	public class NxButton extends NxUIComponent implements IButton
	{
		//内部数据===============================================================
		
		/**
		 * 按钮皮肤区域
		 * */
		private var _btnSkinSpace:Sprite;
		
		/**
		 * 按钮label区域
		 * */
		private var _labelSpace:Sprite;
		
		/**
		 * 按钮图标容器
		 * */
		private var _btnIconSpace:Sprite;
		
		/**
		 * 热区
		 * */
		private var _hotAreaSpace:Sprite;
		
		/**
		 * 按钮一般状态
		 * */
		private var _buttonNormal:Sprite;
		
		/**
		 * 鼠标经过状态
		 * */
		private var _buttonOver:Sprite;
		
		/**
		 * 鼠标按下状态
		 * */
		private var _buttonDown:Sprite;
		
		/**
		 * 禁用状态
		 * */
		private var _btnUnallowed	:Sprite;
		
		/**
		 * 热区是否已经创建
		 * */
		private var _hotAreaSpaceCreated	:Boolean	=	false;
		
		/**
		 * 按钮皮肤是否已经创建
		 * */
		private var _btnSkinCreated		:Boolean	=	false;
		
		/**
		 * label文本框
		 * */
		private var _labelTxt				:TextField	=	null;
		
		//======================================================================
		
		/**
		 * 标题
		 * */
		private var _label			:String;
		
		/**
		 * 按钮皮肤的绑定类名
		 * */
		private var _skin			:String;
		
		/**
		 * 按钮皮肤宽度
		 * */
		private var _skinWidth		:uint			=	80;
		
		/**
		 * 按钮皮肤高度
		 * */
		private var _skinHeight	:uint			=	25;
		
		/**
		 * 是否根据文字大小缩放按钮皮肤。
		 * */
		private var _autoSkinScale	:Boolean		=	false;
		
		/**
		 * 按钮文本的水平对齐方式
		 * */
		private var _horizontalLayout	:String		=	HorizontalLayout.CENTER;
		
		/**
		 * 按钮文本的垂直对齐方式
		 * */
		private var _verticalLayout	:String		=	VerticalLayout.MIDDLE;
		
		/**
		 * 文字的水平偏移值。
		 * */
		private var _offsizeX			:int		=	0;
		
		/**
		 * 文字的垂直偏移值
		 * */
		private var _offsizeY			:int		=	0;
		
		/**
		 * 按钮图标
		 * */
		private var _icon				:Object;
		
		/**
		 * 按钮图标容器
		 * */
		private var _iconContent		:NxClip;
		
		/**
		 * 按钮文字颜色
		 * */
		private var _labelColor		:uint 		= 0xFFC861;
		
		/**
		 * 按钮当前状态
		 * */
		private var _currentState		:String 	= NxButtonState.NORMAL;
		
		/**
		 * 是否活动
		 * */
		private var _enabled			:Boolean 	= true;
		
		private var _glow			:GlowFilter;
		
		/**
		 * 
		 * @param label 按钮文字
		 * @param width 宽度
		 * @param height 高度
		 * @param skin 皮肤
		 * @param top 上偏移量
		 * @param icon icon显示对象/url
		 * @param iconX x坐标
		 * @param iconY y坐标
		 * 
		 */
		//		public function NxButton(label:String = "Button",width:Number=80,height:Number=27,skin:String = "Button_3",top:int = 0,icon:* = null,iconX:int = 0,iconY:int = 0)
		public function NxButton()
		{
			this.buttonMode = true;			
			init();
			
			//			_iconContentView = icon;
			//			_iconContentX = iconX;
			//			_iconContentY = iconY;
			//			
			//			_label	=	label;
			//			_top = top - 4;
			//			
			//			_width	=	width;
			//			_height	=	height;
			//			
			//			_skin = skin;
			
			//			drawBG();
			
			//			drawIcon(icon,iconX,iconY);
		}
		
		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			if(_label != value)
			{
				htmlStrByBorder = value;
			}			
		}
		
		public function set htmlStrByBorder(value:String):void
		{
			var filter:BitmapFilter 		= 	getGlowFilter();
			var myFilters:Array 			= 	new Array();
			myFilters.push(filter);
			_labelTxt.filters = myFilters;
			
			if(_label != value && value != null)
			{
				_label = value;
				_labelTxt.htmlText	=	value;				
				updataLabelProperties();
			}	
		}
		
		private function getGlowFilter():GlowFilter
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	4;
			glow.quality			= 	BitmapFilterQuality.HIGH;
			return glow;
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(val:String):void
		{
			if(_skin != val)
			{
				_skin 			=	val;
				_btnSkinCreated	=	false;
				drawBG();
				updataLabelProperties()
			}
		}
		
		public function get skinHeight():uint
		{
			return _skinHeight;
		}
		
		public function set skinHeight(value:uint):void
		{
			if(_skinHeight != value && _autoSkinScale == false)
			{
				_skinHeight = value;
				updataSkin();
			}			
		}
		
		public function get skinWidth():uint
		{
			return _skinWidth;
		}
		
		public function set skinWidth(value:uint):void
		{
			if(_skinWidth != value && _autoSkinScale == false)
			{
				_skinWidth = value;
				updataSkin();
			}
		}
		
		public function get autoSkinScale():Boolean
		{
			return _autoSkinScale;
		}
		
		public function set autoSkinScale(value:Boolean):void
		{
			if(_autoSkinScale != value)
			{
				_autoSkinScale = value;
				
				if(_autoSkinScale)
				{
					_labelTxt.autoSize	=	TextFieldAutoSize.LEFT;
					
					//重设宽度
					_skinWidth = _labelTxt.textWidth + _offsizeX * 2;
					
					//重设宽度
					_skinHeight = _labelTxt.textHeight + _offsizeY * 2;					
				}
				updataSkin();
				updataLabelProperties();
			}
		}
		
		public function get verticalLayout():String
		{
			return _verticalLayout;
		}
		
		public function set verticalLayout(value:String):void
		{
			if(_verticalLayout != value && !_autoSkinScale)
			{
				_verticalLayout = value;
				updataLabelProperties();
			}
		}
		
		public function get horizontalLayout():String
		{
			return _horizontalLayout;
		}
		
		public function set horizontalLayout(value:String):void
		{
			if(_horizontalLayout != value && !_autoSkinScale)
			{
				_horizontalLayout = value;
				updataLabelProperties();
			}
		}
		
		public function get offsizeX():int
		{
			return _offsizeX;
		}
		
		public function set offsizeX(value:int):void
		{
			if(_offsizeX != value)
			{
				_offsizeX = value;
				updataLabelProperties();
			}
		}
		
		public function get offsizeY():int
		{
			return _offsizeY;
		}
		
		public function set offsizeY(value:int):void
		{
			if(_offsizeY != value)
			{
				_offsizeY = value;
				updataLabelProperties();
			}
		}
		
		public function get icon():Object
		{
			return _icon;
		}
		
		public function set icon(value:Object):void
		{
			if(_icon != value)
			{
				_icon = value;
				drawIcon(value)
			}			
		}
		
		public function set glowColor( value : int ):void
		{
			_glow = new GlowFilter(value, 1, 2, 2, 3, BitmapFilterQuality.LOW);
			
			_labelTxt.filters = [_glow];
		}
		
		public function get glowColor():int
		{
			return _glow.color;
		}
		
		public function get iconContent():IClip
		{
			return _iconContent;
		}
		
		public function set labelColor(color:uint):void
		{
			if(_labelColor != color)
			{
				_labelColor = color;
				updataLabelProperties();
			}
		}
		
		public function get labelColor():uint
		{
			return _labelColor;
		}
		
		public function get currentState():String
		{
			return _currentState;
		}
		
		public function set currentState(value:String):void
		{
			_currentState = value;
			updataBtnState();
		}
		
		public function get enabled():Boolean
		{
			return _enabled;
		}
		public function set enabled(value:Boolean):void
		{
			if(_enabled != value)
			{
				_enabled = value;
				
				if(_enabled)
				{
					currentState = NxButtonState.NORMAL;
					addListener();
					this.mouseEnabled = this.mouseChildren = true;
				}
				else
				{
					currentState = NxButtonState.UNALLOWED;
					removeListener();
					this.mouseEnabled = this.mouseChildren = false;
					
				}
				
				updataLabelProperties();
			}
		}
		
		//不推荐使用的接口=======================================================
		
		override public function get width():Number
		{
			return _skinWidth;
		}
		
		override public function set width(value:Number):void
		{
			if(_autoSkinScale == false)
			{
				_skinWidth = value;
				updataSkin();
			}
		}
		
		override public function get height():Number
		{
			return _skinHeight;
		}
		
		override public function set height(value:Number):void
		{
			if(_autoSkinScale == false)
			{
				_skinHeight = value;
				updataSkin();
			}	
		}
		
		public function get autoSize():Boolean
		{
			return _autoSkinScale;
		}
		
		public function set autoSize(value:Boolean):void
		{
			autoSkinScale	=	value;
		}
		
		public function get horizontalOffSize():uint
		{
			return _offsizeX;
		}
		
		public function set horizontalOffSize(value:uint):void
		{
			if(_offsizeX != value)
			{
				_offsizeX = value;
				updataLabelProperties();
			}
		}
		
		public function get verticalOffSize():uint
		{
			return _offsizeY;
		}
		
		public function set verticalOffSize(value:uint):void
		{
			if(_offsizeY != value)
			{
				_offsizeY = value;
				updataLabelProperties();
			}
		}
		
		//=====================================================================
		
		private function init():void
		{
			//创建容器
			createStateSpace();			
			createIconSpace();
			createHotAreaSpace();			
			drawHotArea();
			createLabel();
			addListener();
			
			label			=	"Button";
			skin			=	"Button_1";
			
			updataSkin();
			
			//			autoSkinScale	=	_autoSkinScale;
			//			labelColor		=	_labelColor;
			
		}
		
		private function drawBG():void
		{
			if(!_btnSkinCreated)
			{
				if(_skin != null && _skin != "")
				{
					//如果有资源,则创建按钮皮肤
					var mc:MovieClip = ResManager.newResClass(_skin) as MovieClip;
					
					if(mc != null)
					{
						_buttonNormal 	= mc.getChildByName("Button_normal") as Sprite;
						_buttonOver 	= mc.getChildByName("Button_over") as Sprite;
						_buttonDown 	= mc.getChildByName("Button_down") as Sprite;
						_btnUnallowed 	= mc.getChildByName("Button_unallowed") as Sprite;
						
						updataBtnState();
						
						_btnSkinCreated	=	true;
					}
				}
			}
		}
		
		/**
		 * 更新按钮皮肤的大小及位置。
		 * */
		private function updataBtnSkinProperties():void
		{
			if(_btnSkinCreated)
			{
				_buttonNormal.x = _buttonOver.x = _buttonDown.x = _btnUnallowed.x = 0;
				_buttonNormal.y = _buttonOver.y = _buttonDown.y = _btnUnallowed.y = 0;
				_buttonNormal.width = _buttonOver.width = _buttonDown.width = _btnUnallowed.width = _skinWidth;
				_buttonNormal.height = _buttonOver.height = _buttonDown.height = _btnUnallowed.height = _skinHeight;
			}			
		}
		
		private function changeToBitMap(mc:Sprite):BitmapData
		{
			var bitMapData:BitmapData = new BitmapData(mc.width,mc.height,true,0);
			bitMapData.draw(mc);
			return bitMapData;
		}
		
		private function drawHotArea():void
		{
			if(!_hotAreaSpaceCreated)
			{
				_hotAreaSpace.graphics.clear();
				_hotAreaSpace.graphics.beginFill(0xff0000,0);
				_hotAreaSpace.graphics.drawRect(0,0,1,1);
				_hotAreaSpace.graphics.endFill();
				_hotAreaSpaceCreated	=	true;
			}
			_hotAreaSpace.width			=	_skinWidth;
			_hotAreaSpace.height		=	_skinHeight;
		}
		
		/**热区**/
		private function createHotAreaSpace():void
		{
			_hotAreaSpace = new Sprite();
			this.addChild(_hotAreaSpace);
		}
		
		/**按钮状态区域**/
		private function createStateSpace():void
		{
			_btnSkinSpace = new Sprite();
			_btnSkinSpace.mouseChildren = _btnSkinSpace.mouseEnabled = false;
			this.addChild(_btnSkinSpace);
		}
		
		private function createIconSpace():void
		{
			_btnIconSpace = new Sprite();
			_btnIconSpace.mouseChildren = _btnIconSpace.mouseEnabled = false;
			this.addChild(_btnIconSpace);
		}
		
		/**按钮label区域**/
		private function createLabel():void
		{
			_labelSpace = 	new Sprite();
			_labelSpace.mouseChildren = _labelSpace.mouseEnabled = false;
			this.addChild(_labelSpace);
			
			_labelTxt	=	new TextField();
			updataLabelProperties();
			
			_labelSpace.addChild(_labelTxt);
			
			glowColor = parseInt(StaticIni.getIniVar("Css_control_nxButton", "glowColor"));
		}
		
		/**
		 * 设置文本属性。
		 * */
		private function updataLabelProperties():void
		{
			_labelTxt.autoSize	=	TextFieldAutoSize.LEFT;
			
			//			_labelTxt.border	=	true;
			
			if(_autoSkinScale)
			{
				_labelTxt.x				=	_offsizeX;
				_labelTxt.y				=	_offsizeY;
				
				_skinWidth				=	_offsizeX*2+_labelTxt.width;
				_skinHeight				=	_offsizeY*2+_labelTxt.height;
				
				drawHotArea();
				updataBtnSkinProperties();
			}
			else
			{
				drawHotArea();
				updataBtnSkinProperties();
				
				switch(_horizontalLayout)
				{
					case HorizontalLayout.LEFT:
						_labelTxt.x		=	_offsizeX;						
						break;
					case HorizontalLayout.CENTER:
						_labelTxt.x		=	0.5*(_skinWidth - _labelTxt.width)+_offsizeX;
						break;
					case HorizontalLayout.RIGHT:
						_labelTxt.x		=	_skinWidth - _labelTxt.width - _offsizeX;
						break;
				}
				
				switch(_verticalLayout)
				{
					case VerticalLayout.TOP:
						_labelTxt.y		=	_offsizeY;
						break;
					case VerticalLayout.MIDDLE:
						_labelTxt.y		=	0.5*(_skinHeight - _labelTxt.height)+_offsizeY;
						break;
					case VerticalLayout.BOTTOM:
						_labelTxt.y		=	_skinHeight - _labelTxt.height - _offsizeY;
						break;
				}
			}
			
			if(_enabled)
			{
				_labelTxt.textColor	=	_labelColor;
			}
			else
			{
				_labelTxt.textColor = 	0x9f9f9f;
			}
			
			_labelTxt.multiline		=	false;
			_labelTxt.mouseEnabled	=	false;
			_labelTxt.type			=	TextFieldType.DYNAMIC;
		}
		
		/**
		 * 
		 * @param view 显示对象/url
		 * @param dx
		 * @param dy
		 * 
		 */
		private function drawIcon(view:*,dx:int = 0 , dy:int = 0):void
		{
			if(_iconContent != null){
				_iconContent.clear();
				_iconContent = null;
				clearAll(_btnIconSpace);
			}
			if(view != null && view != "")
			{
				_iconContent = new NxClip();
				_iconContent.source = view;
				_btnIconSpace.addChild(_iconContent);
				_btnIconSpace.x	= dx;
				_btnIconSpace.y	= dy;
			}
		}
		
		private function addListener():void
		{
			_hotAreaSpace.addEventListener(MouseEvent.ROLL_OUT,onOutHandle);
			_hotAreaSpace.addEventListener(MouseEvent.ROLL_OVER,onOverHandle);
		}
		
		private function addMouseListener():void
		{
			_hotAreaSpace.addEventListener(MouseEvent.CLICK,onClickHandle);
			_hotAreaSpace.addEventListener(MouseEvent.MOUSE_DOWN,onDownHandle);
			_hotAreaSpace.addEventListener(MouseEvent.MOUSE_UP,onUpHandle);
		}
		
		private function removeMoseListener():void
		{
			_hotAreaSpace.removeEventListener(MouseEvent.CLICK,onClickHandle);
			_hotAreaSpace.removeEventListener(MouseEvent.MOUSE_DOWN,onDownHandle);
			_hotAreaSpace.removeEventListener(MouseEvent.MOUSE_UP,onUpHandle);
		}
		
		public function removeListener():void
		{
			_hotAreaSpace.removeEventListener(MouseEvent.ROLL_OUT,onOutHandle);
			_hotAreaSpace.removeEventListener(MouseEvent.ROLL_OVER,onOverHandle);
		}
		
		private function onClickHandle(e:MouseEvent):void
		{
			e.stopPropagation();
			dispatchEvent(e);
		}
		
		private function onOutHandle(e:MouseEvent):void
		{
			e.stopPropagation();
			_currentState = NxButtonState.NORMAL;
			updataBtnState();
			dispatchEvent(e);
			
			removeMoseListener();
		}
		
		private function onOverHandle(e:MouseEvent):void
		{
			e.stopPropagation();
			_currentState = NxButtonState.OVER;
			updataBtnState();
			dispatchEvent(e);
			
			addMouseListener();
		}
		
		private function onDownHandle(e:MouseEvent):void
		{
			e.stopPropagation();
			_currentState = NxButtonState.DOWN;
			updataBtnState();
			dispatchEvent(e);
		}
		
		private function onUpHandle(e:MouseEvent):void
		{
			e.stopPropagation();
			_currentState = NxButtonState.OVER;
			updataBtnState();
			dispatchEvent(e);
		}
		
		/**
		 * 清空
		 */
		private function clearAll(space:Sprite):void
		{
			if(space == null)return; 
			while(space.numChildren > 0){
				space.removeChildAt(0);	
			}
		}
		
		/**
		 * 更新按钮状态显示皮肤
		 */
		private function updataBtnState():void
		{
			var stateSkin : Sprite;
			switch(_currentState){
				case NxButtonState.NORMAL:
					stateSkin = _buttonNormal;
					break;
				case NxButtonState.OVER:
					stateSkin = _buttonOver;
					break;
				case NxButtonState.DOWN:
					stateSkin = _buttonDown;
					break;
				case NxButtonState.UNALLOWED:
					stateSkin = _btnUnallowed;
					break;
			}
			
			if(stateSkin != null)
			{
				clearAll(_btnSkinSpace);
				
				_btnSkinSpace.addChild(stateSkin);
			}
		}
		
		private function inputHandler(e:Event):void
		{
			updataSkin();
		}
		
		private function updataSkin():void
		{
			drawHotArea();
			updataBtnSkinProperties();
			updataLabelProperties();
		}
	}
}